var RefundsTable = {
    id: "refundsTable",//表格id
    me : ""
};


/**
 * 初始化整个表格
 */
RefundsTable.init = function(){
    var defaultColunms = this.initColumn();
    var bstable = new BSTable(RefundsTable.id,Feng.ctxPath+"/refunds/allParam",defaultColunms);
    bstable.setData({});
    this.me = bstable.init();
};

/**
 * 初始化表格的列
 */
RefundsTable.initColumn = function(){
    var columns = [
        {title: '序号',align: 'center',valign: 'middle',
            formatter: function indexFormatter(value, row, index) {//在表格前加上序号
                return index+1;
            }},
        {title: '订单编号',field: 'orderNum',align: 'center',valign: 'middle'},
        {title: '退款学员', field: 'name', align: 'center', valign: 'middle'},
        {title: '退款课程', field: 'refundCourseName', align: 'center', valign: 'middle'},
        {title: '申请时间', field: 'refundsDate', align: 'center', valign: 'middle'},
        {title: '退款理由', field: 'refundsReason', align: 'center', valign: 'middle', cellStyle: formatTableUnit},
        {title: '退款金额', field: 'refundsPrice', align: 'center', valign: 'middle'},
        {title: '退款状态',field: 'refundsStatus',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                if (value === "3") {
                    return "已退款";
                } else if (value === "2") {
                    return "退款中";
                }
            }},
        {title: '经办人', field: 'opUserName', align: 'center', valign: 'middle'},
        {title: '操作',field: '#',align: 'center',valign: 'middle',
            formatter:function(value,row,index){
                var e = '<button class="btn btn-primary btn-xs" onclick="detailBtn(\'' + row.id + '\')" title="详情"><i class="glyphicon glyphicon-pencil"></i></button> &nbsp;&nbsp;';
                var f = '<button class="btn btn-success btn-xs" onclick="refundsAudit(\'' + index + '\')" title="退款审核"><i class="glyphicon glyphicon-ok"></i></button> &nbsp;&nbsp;';
                var g = '<button class="btn btn-danger btn-xs" onclick="cancelRefunds(\'' + row.id + '\')" title="取消退款"><i class="glyphicon glyphicon-remove"></i></button> &nbsp;&nbsp;';
                if (row.refundsStatus === "3") {
                    return e
                } else {
                    return e + f + g;
                }
            }
        }];
    return columns;
};

/**
 * 表格列样式
 */
function formatTableUnit() {
    return {
        css: {
            "overflow": 'hidden',
            "text-overflow": 'ellipsis',
            "white-space": 'nowrap'
        }
    };
}

/**
 * 点击搜索按钮
 */
function clickSearch(){
    var queryData = {};
    //订单号
    var val_search_orderNum = $("#txt_search_orderNum").val();
    if (val_search_orderNum !== "" && val_search_orderNum !== null) {
        queryData['orderNum'] =  val_search_orderNum;
    }
    //退款人
    var val_search_name = $("#txt_search_name").val();
    if (val_search_name !== "" && val_search_name !== null) {
        queryData['name'] = val_search_name;
    }

    //退款状态
    var val_search_refundsStatus = $("#txt_search_refundsStatus").find("option:selected").val();
    if (val_search_refundsStatus !== "" && val_search_refundsStatus !== null && val_search_refundsStatus !== "-1") {
        queryData['refundsStatus'] = val_search_refundsStatus;
    }

    //退款时间
    var val_search_refundsDate = $("#txt_search_refundsDate").val();
    if (val_search_refundsDate !== "" && val_search_refundsDate !== null) {
        queryData['refundsDate'] = val_search_refundsDate;
    }
    RefundsTable.me.destroy(queryData);
    // RefundsTable.me.refresh({query: queryData});
}

/**
 * 清空搜索栏
 */
function resetSearchVal() {
    //订单号
    $("#txt_search_orderNum").val("");
    //退款人
    $("#txt_search_name").val("");
    //退款状态
    $("#txt_search_refundsStatus").val("-1");
    //退款时间
    $("#txt_search_refundsDate").val("");
}

/**
 * 表格编辑按钮，弹出退款详情对话框
 */
function detailBtn(uid) {
    var index = layer.open({
        type: 1,
        title: '退款单信息',
        skin: 'layui-layer-lan',
        area: [RefundsInfoDlg.width, RefundsInfoDlg.height],
        content: RefundsInfoDlg.cache,
        success: function (layero, index) {
            //层弹出成功后回调方法
            RefundsInfoDlg.renderAdd();
            RefundsInfoDlg.load(uid);
        },
        maxmin: true,
        scrollbar: false,
        btn: ['关闭'],
        btnAlign: 'r',  //默认值
        btn2: function (index) {
            layer.close(index);
        },
        cancel: function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    RefundsInfoDlg.currentLayerIndex = index;
}

/**
 * 表格退款审核功能，弹出退款对话框
 */
function refundsAudit(index) {
    var rowData = $("#" + RefundsTable.id).bootstrapTable('getData')[index];
    var index = layer.open({
        type: 1,
        title: '退款审核',
        skin: 'layui-layer-lan',
        area: [RefundsAuditDlg.width, RefundsAuditDlg.height],
        content: RefundsAuditDlg.cache,
        success: function (layero, index) {
            //层弹出成功后回调方法
            RefundsAuditDlg.renderAdd();
            RefundsAuditDlg.refundsAudit(rowData);
        },
        maxmin: true,
        scrollbar: false,
        btn: ['保存', '关闭'],
        btnAlign: 'r',  //默认值
        yes: function (index, layero) {
            RefundsAuditDlg.refundsAuditSubmit();
            layer.close(index); //如果设定了yes回调，需进行手工关闭
        }, btn2: function (index) {
            layer.close(index);
        },
        cancel: function (index, layero) {
            //右上角关闭按钮触发的回调
            layer.close(index);
        }
    });
    RefundsInfoDlg.currentLayerIndex = index;
}

/**
 * 表格取消退款功能，弹出对话框
 */
function cancelRefunds(id) {
    layer.confirm('您确定取消该退款申请?', {
        btn: ['确定', '关闭'] //按钮
    }, function (index) {
        layer.close(index);
        RefundsAuditDlg.cancelRefunds(id);
        RefundsTable.me.refresh();
    });
}

$(function(){
    //初始化退款管理表格
    RefundsTable.init();
    RefundsInfoDlg.init();
    RefundsAuditDlg.init();
});
